<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="顾客姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入顾客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择订单时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单状态" prop="orderStatusId">
    <el-select
      v-model="queryParams.orderStatusId"
      placeholder="请选择订单状态"
      clearable
      @change="handleQuery"
    >
      <el-option
        v-for="item in statusOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
</el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="merchant_orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNumber" />
      <el-table-column label="顾客姓名" align="center" prop="customerName">
        <template v-slot="scope">
          {{ scope.row.customerName || '线下点单' }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" width="120"/>
      <el-table-column label="订单时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单总价" align="center" prop="orderPrice" />
      <el-table-column label="所获积分" align="center" prop="orderPoint" />
      <el-table-column label="订单状态" align="center">
      <template v-slot="scope">
        <span>{{ formatOrderStatus(scope.row.orderStatusId) }}</span>
      </template>
    </el-table-column>
      <el-table-column label="评价" align="center" prop="orderComment" />
      <el-table-column label="叫餐号" align="center" prop="orderCallnum" />
      <!-- <el-table-column label="商家id" align="center" prop="merchantId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">



          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleSelectDetail(scope.row)"
            v-hasPermi="['system:merchant_order:edit']"
          >查看详情</el-button>


        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


   <!-- 订单详情 -->
   <template>
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
    <div>
      <el-spinner v-if="loading" size="large" class="spinner"></el-spinner>
      <el-container v-else>
        <!-- 显示订单基本信息 -->
        <div class="order-info" v-if="tbCustomerOrderDetailList.length > 0" style="margin-bottom: 20px; border-bottom: 2px solid #f5f5f5; padding: 20px; background-color: #fafafa;">
          <h3 style="font-size: 20px; color: #333;">订单号: {{ tbCustomerOrderDetailList[0].orderNumber }}</h3>
          <p style="font-size: 16px; color: #666;">时间: {{ tbCustomerOrderDetailList[0].orderTime }}</p>
          <p style="font-size: 16px; color: #666;">积分: {{ tbCustomerOrderDetailList[0].orderPoint }}</p>

          <el-row :gutter="20" style="margin-top: 20px;">
            <div v-for="(product, productIndex) in tbCustomerOrderDetailList" :key="'product-' + productIndex" class="product-info" style="margin-left : 10px ;padding: 5px;">
            <h4 style="font-size: 18px; color: #333; margin: 0;">{{ product.productName }}</h4>
            <p style="font-size: 14px; color: #000; margin: 5px 0 0;">{{ product.specifications }} 份   {{ product.productNum }} 件</p>

          </div>
        </el-row>

        </div>

        <!-- 遍历所有订单中的每个商品 -->

      </el-container>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel" type="primary">关闭</el-button>
    </div>
  </el-dialog>
</template>

  </div>
</template>

<script>
import { listMerchant_order, getMerchant_order, delMerchant_order, addMerchant_order, updateMerchant_order ,getMerchant_order_detail } from "@/api/system/merchant_order";

export default {
  name: "Merchant_order",
  data() {
    return {
      statusOptions: [
      { label: '未支付', value: 5 },
      { label: '已支付', value: 6 },
      { label: '已过期', value: 7 },
      { label: '已取消', value: 8 },
      { label: '已完成', value: 9 }
    ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbCustomerOrderDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      merchant_orderList: [],
      // 商家-顾客：订单详情

      tbCustomerOrderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        customerName: null,
        orderTime: null,
        orderPrice: null,
        orderPoint: null,
        orderStatusId: null,
        orderComment: null,
        orderCallnum: null,
        merchantId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderTime: [
          { required: true, message: "订单时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
  getOrderDetail(){

  },
  /** 查看详情按钮操作 */
  handleSelectDetail(row) {
  this.reset();
  this.title = "订单详情";
  this.tbCustomerOrderDetailList = [];

  getMerchant_order_detail(row.orderNumber).then(response => {
    this.tbCustomerOrderDetailList = response.data;
    this.$nextTick(() => {
      this.open = true; // 确保数据加载完成后再打开弹窗
    });
    console.log(this.tbCustomerOrderDetailList);
  });
},
    //状态显示
    formatOrderStatus(statusId) {
      switch (statusId) {
        case 5:
          return '未支付';
        case 6:
          return '已支付';
        case 7:
          return '已过期';
        case 8:
          return '已取消';
        case 9:
          return '已完成';
        default:
          return '未知状态';
      }
    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listMerchant_order(this.queryParams).then(response => {
        this.merchant_orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      console.log(this.merchant_orderList)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNumber: null,
        customerId: null,
        orderTime: null,
        orderPrice: null,
        orderPoint: null,
        orderStatusId: null,
        orderComment: null,
        orderCallnum: null,
        merchantId: null
      };
      this.tbCustomerOrderDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderNumber)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderNumber = row.orderNumber || this.ids
      getMerchant_order(orderNumber).then(response => {
        this.form = response.data;
        this.tbCustomerOrderDetailList = response.data.tbCustomerOrderDetailList;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbCustomerOrderDetailList = this.tbCustomerOrderDetailList;
          if (this.form.orderNumber != null) {
            updateMerchant_order(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMerchant_order(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderNumbers = row.orderNumber || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + orderNumbers + '"的数据项？').then(function() {
        return delMerchant_order(orderNumbers);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 商家-顾客：订单详情

    rowTbCustomerOrderDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 商家-顾客：订单详情  */
    handleAddTbCustomerOrderDetail() {
      let obj = {};
      obj.productName = "";
      obj.productNum = "";
      this.tbCustomerOrderDetailList.push(obj);
    },


    /** 复选框选中数据 */
    handleTbCustomerOrderDetailSelectionChange(selection) {
      this.checkedTbCustomerOrderDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/merchant_order/export', {
        ...this.queryParams
      }, `merchant_order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.order-card {
  background-color: #fafafa;
  border: 1px solid #e4e4e4;
  border-radius: 8px;
  padding: 20px;
}

.order-info {
  border-bottom: 2px solid #f5f5f5;
  padding-bottom: 10px;
  margin-bottom: 20px;
}

.product-list {
  margin-top: 20px;
}

.product-item {
  margin-bottom: 10px;
}
</style>
